package com.tansuo365.test1.controller.excelcontrol;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.tansuo365.test1.bean.otherbean.DProject;
import com.tansuo365.test1.bean.otherbean.DWuliao;
import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.listener.UploadProjectDataListener;
import com.tansuo365.test1.listener.UploadSengouDetailsDataListener;
import com.tansuo365.test1.listener.UploadWuliaoDataListener;
import com.tansuo365.test1.service.excelservice.IAllDatasService;
import com.tansuo365.test1.service.excelservice.ProjectExcelService;
import com.tansuo365.test1.service.excelservice.WuliaoExcelService;
import com.tansuo365.test1.service.otherservice.impl.ShengouDetailsCacheServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//import com.institutecloud.msfileservice.bean.goods_price.Pcoke;

//excel导入控制层

/**
 * 适用于导入物料
 */
@RequestMapping("/excel")
@RestController
public class ExcelImportController {
//    private static final Logger LOGGER =
//            LoggerFactory.getLogger(ExcelImportController.class);


    @Autowired
    private WuliaoExcelService wuliaoExcelService;
    @Autowired
    private ProjectExcelService projectExcelService;
    @Autowired
    private ShengouDetailsCacheServiceImpl shengouDetailsCacheService;

    private IAllDatasService service;

    public void setService(IAllDatasService service){
        this.service = service;
    }
    public IAllDatasService getService() {
        return service;
    }

    private Class clazzz;

    public void setClazz(Class clazz){
        clazzz = clazz;
    }
    public Class getClazz(){
        return clazzz;
    }


    /**
     * 通过easyexcel的读取excel方法获取excel信息,通过对
     * 文件的流读取 excel.getInputStream()
     * 货品类class的设定 getClazz() *(之前通过了switch判定goodsType)
     * 上传数据监听器new UploadWuliaoDataListener(getService())spring注入进来的货品的service层放入,在监听器中使用service接口进行统一管理接收
     * @param theExcel 前端传入excel文件
     * @param type 导入的类型 如wuliao,shengoudan
     * @param wRoot 物料根类型
     * @param oSub 物料子类型
     *
     * @throws IOException
     */

    @PostMapping("/importExcel/{type}/{wRoot}/{oSub}")
    public int importExcel(@RequestParam(value = "theExcel") MultipartFile theExcel, @PathVariable String type,
                           @PathVariable String wRoot,@PathVariable String oSub) throws IOException {
//        LOGGER.info("进入importExcel方法,将执行excel导入.");
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("wRoot",wRoot);
        paramsMap.put("oSub",oSub);
        switch (type){
            case "wuliao" :
                setClazz(DWuliao.class);
                setService(wuliaoExcelService);
                break;
            case "xiangmu" :
                setClazz(DProject.class);
                setService(projectExcelService);
                break;
//            case "anodePrice" : setClazz(AnodePrice.class); setService(anodePriceService);break;
            default:break;
        }
        try {
            if (type.equals("wuliao")){
                EasyExcel.read(theExcel.getInputStream(), getClazz(), new UploadWuliaoDataListener(getService(),paramsMap)).sheet().doRead();
                return 1; //读取excel成功
            }
            if (type.equals("xiangmu")){
                EasyExcel.read(theExcel.getInputStream(), getClazz(), new UploadProjectDataListener(getService(),paramsMap)).sheet().doRead();
                return 1; //读取excel成功
            }
        } catch (IOException e) {
            e.printStackTrace();
            return 0; //读取excel失败
        }
        return 0; //读取excel失败

    }

    /*excel批量导入申购详情并回显到页面接口*/
    @PostMapping("/importExcel/wuliaoshengoudanmingxi")
    public Map<String,Object> importExcel(@RequestParam(value = "theExcel") MultipartFile theExcel,
                                          @RequestParam(value="page",required=true)Integer page, @RequestParam(value="rows",required =true)Integer rows) throws IOException {
        setClazz(ShengouDetails.class);
        setService(wuliaoExcelService);
        List<ShengouDetails> returnShengouDetailsList = new ArrayList<>();

        Map<String,Object> resultMap = new HashMap<>();

        try {
            EasyExcel.read(theExcel.getInputStream(), getClazz(), new UploadSengouDetailsDataListener(getService(),shengouDetailsCacheService,returnShengouDetailsList)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<ShengouDetails> shengouDetailsCacheList = shengouDetailsCacheService.selectAll();
        shengouDetailsCacheService.delectAll();
        System.out.println("数据导出成功"+shengouDetailsCacheList);
        PageInfo<ShengouDetails> pageInfo = new PageInfo<ShengouDetails>(shengouDetailsCacheList);
        resultMap.put("rows", pageInfo.getList());
        resultMap.put("total", pageInfo.getTotal());

        return resultMap;
    }

//    @PostMapping("/importShengouExcel/{type}")
//    public int importShengouExcel(@RequestParam(value = "theExcel") MultipartFile theExcel, @PathVariable String type,
//                           @PathVariable String wRoot,@PathVariable String oSub) throws IOException {
////        LOGGER.info("进入importExcel方法,将执行excel导入.");
////        Map<String,Object> paramsMap = new HashMap<>();
////        paramsMap.put("wRoot",wRoot);
////        paramsMap.put("oSub",oSub);
//        switch (type){
//            case "shengouxx" : setClazz(ShengouDetails.class); setService(shengouDetailsExcelService);break;
////            case "anodePrice" : setClazz(AnodePrice.class); setService(anodePriceService);break;
//            default:break;
//        }
//        try {
//            EasyExcel.read(theExcel.getInputStream(), getClazz(), new UploadWuliaoDataListener(getService(),paramsMap)).sheet().doRead();
//        } catch (IOException e) {
//            e.printStackTrace();
//            return 0; //读取excel失败
//        }
//        return 1; //读取excel成功
//    }

}
